package com.matchmealpha;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FriendsListActivity extends Activity implements OnClickListener {

	public static FriendsListActivity ACTIVE_INSTANCE; 
    public static boolean CURRENTLY_DOWNLOADING; 
	
	private Button leftButton;
	private Button rightButton;
	private TextView errorMsg;
	private RelativeLayout loadingPanel;

	private ListView friendsLayout;
	private List<String> friendsList;
	protected FriendsListAdapter friendsAdapter;

	private boolean back;
	private int callerID; // 1 - recent, 2 - alert, 3 - timeline, 4 - friends
	private int requestcode;

	private String username;
	private String sessionid;

	protected ProgressDialog loadingDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		ACTIVE_INSTANCE = this;
		super.onCreate(savedInstanceState);

		/*** Shared Pref ***/
		SharedPreferences settings = getSharedPreferences(
				"matchme_preferences", 0);
		username = settings.getString("username", "");
		sessionid = settings.getString("sessionid", "");

		/*** Bundle ***/
		Bundle bund = this.getIntent().getExtras();
		if (bund != null) {
			this.back = bund.getBoolean("back");
			this.callerID = bund.getInt("callerID");
			this.requestcode = bund.getInt(ShareSettingActivity.REQUEST_CODE,
					-1);
		} else {
			this.back = false;
			this.callerID = MatchMeTabWidget.FRIENDS;
			Log.d("MATCHME", "SHOULD NOT BE HERE in FriendsListActivity");
		}

		/*** Init ***/
		initLayout();
		initListener();

		/*** List View ***/
		friendsList = MatchMeTabWidget.friendsList;
		MatchMeTabWidget.friendsAdapter = new FriendsListAdapter(this, MatchMeTabWidget.friendsList,
				this.callerID, this.requestcode);
		friendsLayout.setAdapter(MatchMeTabWidget.friendsAdapter);
		friendsLayout.setOnItemClickListener(MatchMeTabWidget.friendsAdapter);
		displayLayout();
		
//		if(friendsList == null)
//		{
//			
//			 MatchMeTabWidget.friendsList = new ArrayList<String>();
//			 friendsList = MatchMeTabWidget.friendsList;
//			friendsAdapter = new FriendsListAdapter(this, friendsList,
//					this.callerID, this.requestcode);
//			friendsLayout.setAdapter(friendsAdapter);
//			friendsLayout.setOnItemClickListener(friendsAdapter);
//			/*** Loading Screen ***/
//			if (requestcode > 0) {
//				loadingDialog = ProgressDialog
//						.show(this, "", "Connecting...", true);
//			} else {
//				loadingDialog = ProgressDialog
//						.show(getParent(), "","Connecting...", true);
//			}
//			loadingDialog.show();
//			new FriendsListTask().execute();
//			
//		}else
//		{
//			
//			friendsAdapter = new FriendsListAdapter(this, friendsList,
//					this.callerID, this.requestcode);
//			friendsLayout.setAdapter(friendsAdapter);
//			friendsLayout.setOnItemClickListener(friendsAdapter);
//			displayLayout();
//			
//		}
		
		

	}

	@Override
	protected void onResume() {
		super.onResume();
		//friendsAdapter.notifyDataSetChanged();
	}

	private void initLayout() {
		setContentView(R.layout.friendslist_view);
		leftButton = (Button) this.findViewById(R.id.FriendslistLeftButton);
		rightButton = (Button) this.findViewById(R.id.FriendslistRightButton);
		errorMsg = (TextView) this.findViewById(R.id.FriendslistError);
		friendsLayout = (ListView) this.findViewById(R.id.FriendslistListView);
		loadingPanel = (RelativeLayout) findViewById(R.id.FriendListLoadingPanel);
		
		if(!CURRENTLY_DOWNLOADING){
			 loadingPanel.setVisibility(View.GONE);
		}

		friendsLayout.setVisibility(View.INVISIBLE);
		errorMsg.setVisibility(View.GONE);
		if (!this.back)
			leftButton.setVisibility(View.INVISIBLE);
		rightButton.setVisibility(View.INVISIBLE);
	}

	private void displayErrorLayout() {
		errorMsg.setVisibility(View.VISIBLE);
		friendsLayout.setVisibility(View.INVISIBLE);
		friendsAdapter.notifyDataSetChanged();
		errorMsg.postInvalidate();

	}

	private void displayLayout() {
		errorMsg.setVisibility(View.GONE);
		friendsLayout.setVisibility(View.VISIBLE);
		//friendsAdapter.notifyDataSetChanged();
		errorMsg.postInvalidate();

	}

	private void initListener() {
		if (this.back)
			leftButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case (R.id.FriendslistLeftButton):
			if (this.callerID == MatchMeTabWidget.SHARE)
				finish();
			else
				MatchMeTabWidget.myBack(this.callerID);
			break;
		}
	}

	private class FriendsListTask extends AsyncTask<String, Void, Boolean> {

		protected Boolean doInBackground(String... data) {

			HttpRequest req = new HttpRequest(sessionid);
			String response;
			response = req.sendPost(
					MatchMeTabWidget.MM_SERVER + "/get_friends", "");
			if (MatchMeTabWidget.USER_AUTH_FAILURE.equals(response))
				return false;
			else if (response != "") {
				Scanner in = new Scanner(response).useDelimiter(";");
				String str;
				friendsList.clear();
				while (in.hasNext()) {
					str = in.next();
					friendsList.add(str);
				}
				return true;
			} else
				return false;
		}

		protected void onPostExecute(Boolean result) {
			loadingDialog.cancel();
			if (result) {
				if (friendsList.size() <= 0) {
					Toast.makeText(getParent(),
							"You currently have no friends", Toast.LENGTH_LONG);
				}
				displayLayout();
			} else {
				errorMsg.setText("Unable to connect. Please try again");
				displayErrorLayout();
			}
		}
	}

	public void hideLoadingPanel() {
		loadingPanel.setVisibility(View.GONE);	
	}
}
